package Dao;

import Pojo.User;
import Utils.DBUtil;
import Utils.isString;
import org.apache.log4j.Logger;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class UserDao {
    private final DBUtil dbUtil = new DBUtil();
    private final isString isString = new isString();
    private final Logger logger = Logger.getLogger(UserDao.class);

    /**
     * 根据手机号密码返回User对象，如果不存在用户则返回null
     *
     * @param telephone 手机号
     * @param password  密码
     * @return User
     * @throws SQLException
     */
    public User getUserByTelephoneAndPassword(String telephone, String password) throws SQLException {
        String sql = "select " +
                "userId," +
                "userNickname," +
                "userTelPhone," +
                "userPassword," +
                "userEmail," +
                "userName," +
                "userRole " +
                "from `user` " +
                "where userTelPhone = ? and userPassword = ?";
        Object[] params = {telephone, password};
        logger.info("sql=>" + sql);
        logger.info("param=>" + Arrays.toString(params));
        return dbUtil.queryData(sql, params, User.class);
    }

    /**
     * 查询邮箱或者手机号是否存在
     *
     * @param tel
     * @param email
     * @return
     * @throws SQLException
     */
    public int getUserCountByTelOrEmail(String tel, String email) throws SQLException {
        String sql = "select " +
                "count(userId) " +
                "from `user` " +
                "where ";
        StringBuffer sb = new StringBuffer();
        sb.append(sql);
        Object[] params = null;
        if (tel != null) {
            params = new Object[]{tel};
            sb.append("userTelPhone = ?");
        } else if (email != null) {
            params = new Object[]{email};
            sb.append("userEmail = ?");
        }
        logger.info("sql=>" + sb.toString());
        logger.info("params=>" + Arrays.toString(params));
        return dbUtil.queryCount(sb.toString(), params);
    }

    /**
     * 插入用户
     *
     * @param tel
     * @param password
     * @param nickname
     * @param email
     * @param username
     * @return
     * @throws SQLException
     */
    public int insertUser(String tel, String password, String nickname, String email, String username) throws SQLException {
        String sql = "insert into " +
                "`user`(userNickname," +
                "userTelPhone," +
                "userPassword," +
                "userEmail," +
                "userName) " +
                "values(?,?,?,?,?)";
        Object[] params = {nickname, tel, password, email, username};
        return dbUtil.update(sql, params);
    }

    /**
     * 根据用户ID修改密码
     *
     * @param userId
     * @param password
     * @return
     * @throws SQLException
     */
    public int updatePasswordByUserId(int userId, String password) throws SQLException {
        String sql = "update `user` set userPassword = ? where userId = ?";
        Object[] params = {password, userId};
        logger.info("sql=>" + sql);
        logger.info("params=>" + Arrays.toString(params));
        return dbUtil.update(sql, params);
    }

    /**
     * 根据用户ID修改信息
     *
     * @param nickname
     * @param email
     * @param username
     * @param userId
     * @return
     * @throws SQLException
     */
    public int updateUserMsgByUserId(String nickname, String email, String username, int userId) throws SQLException {
        String sql = "update `user`" +
                " set " +
                "userNickname = ?," +
                "userEmail = ?," +
                "userName = ? " +
                "where userId = ? ";
        Object[] params = {nickname, email, username, userId};
        logger.info("sql=>" + sql);
        logger.info("params=>" + Arrays.toString(params));
        return dbUtil.update(sql, params);
    }

    /**
     * @param searchCon
     * @param search
     * @return
     * @throws SQLException
     */
    public List<Map<String, Object>> getUsersIsAdminBySelective(int searchCon, String search, int pageIndex) throws SQLException {
        String sql = "select " +
                "userId,userNickname,userTelPhone,userEmail,userName\n" +
                "from " +
                "`user` ";
        StringBuffer sb = new StringBuffer();
        sb.append(sql);
        List<Object> temp = new ArrayList<>();
        if (!isString.IsEmpty(search)) {
            sb.append(" where ");
            if (searchCon == 4) {
                sb.append(" userName like ? ");
            } else if (searchCon == 2) {
                sb.append(" userTelPhone like ? ");
            } else if (searchCon == 3) {
                sb.append(" userEmail like ?");
            } else {
                sb.append(" userId like ?");
            }
            temp.add(temp.size(), search);

        }
        sb.append(" limit ?,10");
        temp.add(temp.size(), pageIndex);
        Object[] params = temp.toArray();
        logger.info("sql=>" + sb.toString());
        logger.info("params=>" + Arrays.toString(params));
        return dbUtil.queryCustoms(sb.toString(), params);
    }

    /**
     * 查询内容返回总数
     *
     * @param searchCon
     * @param search
     * @return
     * @throws SQLException
     */
    public int getUsersCountIsAdminBySelective(int searchCon, String search) throws SQLException {
        String sql = "select " +
                "count(userId) " +
                "from " +
                "`user` ";
        List<Object> temp = new ArrayList<>();
        StringBuffer sb = new StringBuffer();
        sb.append(sql);
        if (!isString.IsEmpty(search)) {
            sb.append(" where ");
            if (searchCon == 4) {
                sb.append(" userName like ? ");
            } else if (searchCon == 2) {
                sb.append(" userTelPhone like ? ");
            } else if (searchCon == 3) {
                sb.append(" userEmail like ?");
            } else {
                sb.append(" userId like ?");
            }
            temp.add(temp.size(), search);

        }
        Object[] params = temp.toArray();
        logger.info("sql=>" + sb.toString());
        logger.info("params=>" + Arrays.toString(params));
        return dbUtil.queryCount(sb.toString(), params);
    }

    /**
     * 删除用户
     *
     * @param userId
     * @return
     * @throws SQLException
     */
    public int deleteUserIsAdminByUserId(String userId) throws SQLException {
        String sql = "delete from `user` where userId = ? and userRole = 1 ";
        Object[] params = {userId};
        logger.info("sql=>" + sql);
        logger.info("params=>" + Arrays.toString(params));
        return dbUtil.update(sql, params);
    }
}
